package com.yztc.orangeentertainment.ui.main.subscribefragment.sublablist;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.yztc.orangeentertainment.bean.LabListBean.LabListBean;
import com.yztc.orangeentertainment.bean.LabListBean.ListBean;
import com.yztc.orangeentertainment.config.SubscribeUrlConfig;
import com.yztc.orangeentertainment.http.SubscribeRetrofitService;
import com.yztc.orangeentertainment.ui.main.subscribefragment.starlist.OnLoadStarListListener;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;

/**
 * Created by My on 2016/10/7.
 */
public class LabListModle implements LabListContract.Modle {
    @Override
    public void getLabListAll(String type, Map<String, String> params, final OnLoadLabListListener loadStarListListener, Context context) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(SubscribeUrlConfig.PATH.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<LabListBean> call=retrofit.create(SubscribeRetrofitService.class).getLabListAll(type,params);
        call.enqueue(new Callback<LabListBean>() {
            @Override
            public void onResponse(Call<LabListBean> call, Response<LabListBean> response) {
                if (response.isSuccessful()){
                    List<ListBean> list=response.body().getData().getList();
                    loadStarListListener.onSuccess(list);
                }else {
                    Log.i("tag","wrong");
                }
            }

            @Override
            public void onFailure(Call<LabListBean> call, Throwable t) {
                loadStarListListener.onFailure((Exception) t);
            }
        });
    }

    @Override
    public void getSearchStar(String type, Map<String, String> params, final Context context, final OnLoadLabListListener loadStarListListener) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(SubscribeUrlConfig.PATH.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<LabListBean> call=retrofit.create(SubscribeRetrofitService.class).getSubLabSearchAll(type,params);
        call.enqueue(new Callback<LabListBean>() {
            @Override
            public void onResponse(Call<LabListBean> call, Response<LabListBean> response) {
                if (response.isSuccessful()){
                    if (response.body().getData()!=null) {
                        List<ListBean> list = response.body().getData().getCategory();
                        loadStarListListener.onSuccess(list);
                    }else {
                        Toast.makeText(context, "没有更多数据了", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Log.i("tag","wrong");
                }
            }

            @Override
            public void onFailure(Call<LabListBean> call, Throwable t) {
                loadStarListListener.onFailure((Exception) t);
            }
        });
    }

    @Override
    public void getLabListFromDb(Subscriber<List<ListBean>> subscriber) {

    }
}
